/*
 * author zby
 * 首页初始化数据
 */

(function (document, window, $, Utils) {
    'use strict';
    var areaId, udrId, id;
    var vm = {};
    $(function () {
        var fns = {
            search: function (me) {
                Utils.getSimpleJson({
                    url: "/core/attach/page",
                    params: Utils.getSearchParams(me),
                    fn: function (data) {
                        vm.attaches = data;
                    }
                })
            },
            define: function () {
                if (!id) {
                    layer.msg("请选择文件!", {time: 1000, icon: 2});
                    return;
                }
                Utils.getSimpleJson({
                    url: "/core/attach/" + id,
                    type: "PUT",
                    params: {
                        areaId: areaId,
                        fileType: $("#fileType").find(":selected").val(),
                        desc: $("#desc").val(),
                        udrId: udrId,
                        id: id
                    },
                    fn: function () {
                        layer.msg("上传成功!", {time: 1000, icon: 2}, function () {
                            $("#PrimaryModalhdbgcl").modal('hide');
                            fns.search();
                        });
                    }
                })
            },
            del: function (cid) {
                Utils.delete({
                    url: "/core/attach",
                    id: cid
                })
            }
        };
        window.fns = fns;
        init();
    });

    //入口方法
    function init() {
        vm = new Vue({
            el: '#tableInfo',
            data: {
                enums: {},
                attaches: {}
            },
        })
        Utils.getSimpleJson({
            url: "/core/enum/list",
            noAsync: false,
            params: {
                path: "FileTypeEnum",
            },
            fn: function (data) {
                vm.enums = data;
            }
        })
        fns.search();
        AMap.plugin('AMap.CitySearch', function () {
            var citySearch = new AMap.CitySearch()
            citySearch.getLocalCity(function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    areaId = result.adcode;
                }
            })
        })
        $("#upload").fileinput({
            language: 'zh', //设置语言
            uploadUrl: constant.server_path + "/core/attach/upload", //上传的地址
            showUpload: true, //显示整体上传的按钮
            showRemove: false,//显示整体删除的按钮
            uploadAsync: true,//默认异步上传
            uploadLabel: "上传",//设置整体上传按钮的汉字
            // removeLabel: "移除",//设置整体删除按钮的汉字
            uploadClass: "btn btn-primary",//设置上传按钮样式
            showCaption: true,//是否显示标题
            allowedFileExtensions: ['doc', 'docx', 'jar','txt'],//接收的文件后缀
            dropZoneEnabled: false,//是否显示拖拽区域
            maxFileCount: 10, //表示允许同时上传的最大文件个数
            enctype: 'multipart/form-data',
            validateInitialCount: true,
            previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
            msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
            ajaxSettings: {
                dataType: 'json',
                success: function (result) {
                    const data = result.resultData;
                    id = data.id;
                    udrId = data.udrId;
                }
            }
        }).on('filesuccessremove', function (event, key, jqXHR, data) {
            layer.confirm("确认删除吗？", {
                btn: ["确认", "取消"], btn1: function (index) {
                    Utils.delete({
                        url: "/core/attach",
                        id: id
                    })
                }, btn2: function (index) {
                    layer.close(index)
                }
            })
        });
    }


})(document, window, jQuery, Utils);